package com.yanxinim.cn.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.NewsAdapter;
import com.yanxinim.cn.bean.news.NewsResultData;
import com.yanxinim.cn.ui.base.EasyFragment;
import com.yanxinim.cn.util.HttpUtil;
import com.yanxinim.cn.util.HttpUtils;
import com.yanxinim.cn.util.JsonTool;
import com.yanxinim.cn.util.JsonUtils;
import com.yanxinim.cn.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

public class NewsListFragment extends EasyFragment {
    NewsResultData newsResultData;
    NewsAdapter goodsAdapter;
    private SmartRefreshLayout srlRefresh;
    private RecyclerView rvNewsList;
    private ImageView ivNothing;
    private Context context;

    private String status;
    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            newsResultData = JsonTool.jsonToObject(String.valueOf(msg.obj), NewsResultData.class);
            if (newsResultData.getResult() != null) {
                goodsAdapter = new NewsAdapter(context, newsResultData.getResult().getData());
                rvNewsList.setAdapter(goodsAdapter);
            } else {
                if (newsResultData.getReason() != null)
                    ToastUtil.showToast(context, newsResultData.getReason());
                else ToastUtil.showToast(context, "数据请求失败");
            }

        }
    };

    public NewsListFragment() {
    }

    public static NewsListFragment getInstance(String status) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int inflateLayoutId() {
        return R.layout.a_news_list_fragment;
    }

    @Override
    protected void onActivityCreated(Bundle savedInstanceState, boolean createView) {
        context = getActivity();
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getString("status");
        }
        srlRefresh = findViewById(R.id.srl_refresh);
        rvNewsList = findViewById(R.id.rv_news_list);
        ivNothing = findViewById(R.id.iv_nothing);
        ivNothing.setVisibility(View.GONE);
        rvNewsList.setLayoutManager(new LinearLayoutManager(context));

        srlRefresh.setEnableLoadMore(false);
        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData(status);
            }
        });
        initData(status);
    }

    public void initData(String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", AppConfig.NEWS_APPKEY);
        if (!(TextUtils.isEmpty(type) || type.equals("tuijian")))
            map.put("type", type);
        HttpUtils.post(AppConfig.NEWS_DATA_URL, map, new HttpUtils.HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                if (!TextUtils.isEmpty(result)) {
                    Message message = new Message();
                    message.what = 1;
                    message.obj = result;
                    handler.sendMessage(message);
                }
                srlRefresh.finishRefresh();
            }

            @Override
            public void onFail(String error) {
                srlRefresh.finishRefresh();
            }
        });
    }
}
